package com.wunong.smart.address.domain.operation;

import com.wunong.smart.address.application.context.UserAddressContext;
import com.wunong.smart.address.repository.UserAddressRepository;
import com.wunong.smart.address.application.request.BaseUserAddressRequest;
import com.wunong.smart.domain.platform.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;

/**
 * @author create by zealot.zt
 */
@Slf4j
public abstract class AbstractUserAddressOperation<REQ extends BaseUserAddressRequest> implements UserAddressOperation<REQ> {

    @Resource
    protected UserAddressRepository userAddressRepository;

    @Override
    public Object operate(UserAddressContext<REQ> context) {
        beforeOperate(context);
        Object result = doOperate(context);
        afterOperate(context, result);
        return result;
    }

    protected void beforeOperate(UserAddressContext<REQ> context) throws ServiceException {
        // 判断能否操作
    }

    /**
     * 执行对应的操作
     *
     * @param context
     * @return
     */
    protected abstract Object doOperate(UserAddressContext<REQ> context);

    protected void afterOperate(UserAddressContext<REQ> context, Object result) {
        // do nothing
    }

}
